package com.fwc.churchtracker.domo;

import com.fwc.fw.annotation.Form;
import com.fwc.fw.annotation.FormField;
import com.fwc.fw.annotation.LookupDomo;
import com.fwc.fw.annotation.LookupField;
import com.fwc.fw.domo.AbstractPersistableDomo;

import java.util.Date;

import javax.jdo.annotations.Discriminator;
import javax.jdo.annotations.DiscriminatorStrategy;
import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.Inheritance;
import javax.jdo.annotations.InheritanceStrategy;
import javax.jdo.annotations.NotPersistent;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;

import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;

@Form(label="Church")
@LookupDomo(filter="active==true")
@PersistenceCapable(identityType = IdentityType.APPLICATION, detachable="true")
@Inheritance(strategy=InheritanceStrategy.NEW_TABLE)
@Discriminator(strategy=DiscriminatorStrategy.CLASS_NAME)
public class Church extends AbstractPersistableDomo {

    @Persistent
    @FormField(label="Church Name", order=10)
    private String name;

    @Persistent
    @LookupField
    private String _ci_name;

    @Persistent(defaultFetchGroup="true")
    private Key adminKey;
    
    @NotPersistent
    @FormField(label="Administrator", controller="/member", renderer="autocomplete", order=40)
    private Member admin;
    
	public String getName() {
		return name;
	}

	public void setName(String name) {
    	if(name!=null) {
    		this._ci_name = name.toLowerCase();
    	} else this._ci_name = null;
		this.name = name;
	}

	public Member getAdmin() {
		return admin;
	}

	public void setAdmin(Member admin) {
		this.admin = admin;
		if(admin==null) {
			setCommishKey(null);
		} else {
			setCommishKey(KeyFactory.stringToKey(admin.getKey()));
		}
	}
	
	public Key getCommishKey() {
		return adminKey;
	}

	public void setCommishKey(Key commishKey) {
		this.adminKey = commishKey;
	}

	public String toString() {
		if(name==null || name.isEmpty()) return "undefined";
		return name;
	}

}